var app = getApp();
var config = require('../../../config.js');
var util = require('../../../js/common.util.js');
Page({
  data: {
    id:'',
    product:{},
    company:{},
    backgrounds: [],
    indicatorDots: true,
    interval: 2000,
    duration: 500,
    currentSwiper: 0,
    imgheights: [],
    imgwidth: 750,
    current: 0,
  },
  imageLoad: function (e) {
    console.log(e)
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;
    //console.log(imgwidth, imgheight)
    //计算的高度值
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights = this.data.imgheights
    //把每一张图片的高度记录到数组里
    imgheights.push(imgheight)
    this.setData({
      imgheights: imgheights,
    })
  },
  onLoad:function(options){
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.loadDetail()
    }
  },
  onShareAppMessage:function(){

  },
  loadDetail: function () {
    let self = this
    app.request({
      url: 'api/wechat/getProductById',
      method: "GET",
      data: {
        id:self.data.id
      },
      success: function (res) {
        console.log(res)
        if(res.data.result){
          var imgList = res.data.product.pictureList
          var tempList = []
          if(imgList != '' && imgList.length>0){
            var list = imgList.split(',')
            for(var i = 0;i<list.length;i++){
              console.log(list[i])
              tempList.push(config.imgPath + list[i])
            }
            self.setData({
              backgrounds: tempList
            })
          }
          var date = res.data.company.registerDate.split(' ')[0]
          res.data.company.registerDate = date
          res.data.company.logo = config.imgPath + res.data.company.logo
          res.data.product.content = res.data.product.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto"')
          self.setData({
            product:res.data.product,
            company:res.data.company
          })
          console.log(self.data.backgrounds)
        }
      }
    })
  }
})